
'''
加密

1.导入
2.创建实例对象
3.组织数据，然后加密
'''

# 1.导入
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

# 2.创建实例对象
# secret_key      秘钥
# expires_in      数据过期时 单位是秒数
s = Serializer(secret_key='666',expires_in=3600)

# 3.组织数据，然后加密
data = {
    'openid':'123456'
}

s.dumps(data)

# b'eyJhbGciOiJIUzUxMiIsImlhdCI6MTYxMDI1OTM2NiwiZXhwIjoxNjEwMjYyOTY2fQ.eyJvcGVuaWQiOiIxMjM0NTYifQ.jkqT44beqN1N7IWfkyvCh_P4KJYQL8fYiiKuKZMNCJ3Sj7jNDOFnzfvAcY46IyxuJ2loHaRTVsDREbSUk5vY9g'

############################
# python manage.py shell 中测试

'''
解密数据  *注意：解密数据是可能会有异常

1. 导入
2. 创建实例对象(参数要与 加密的参数一致)
3. 解密数据
'''
# 1.导入
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from itsdangerous import BadSignature

# 2.创建实例对象(参数要与加密的参数一致)
s = Serializer(secret_key='666',expires_in=3600)

# 3.解密数据
# loads()中的数据也必须是加密的
try:
    s.loads('eyJhbGciOiJIUzUxMiIsImlhdCI6MTYxMDI1OTM2NiwiZXhwIjoxNjEwMjYyOTY2fQ.eyJvcGVuaWQiOiIxMjM0NTYifQ.jkqT44beqN1N7IWfkyvCh_P4KJYQL8fYiiKuKZMNCJ3Sj7jNDOFnzfvAcY46IyxuJ2loHaRTVsDREbSUk5vY9g')
except BadSignature:
    print('加密数据被篡改')

